import {
  Request,Upload,Login
} from "../../utils/request.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //
    typeindex: '-1',
    //高级信息
    fatherList: [],
    heightinfo: [],
    navbarData: {
      showCapsule: 1
    },
    open: 1, //切换基本信息、高级信息、印象
    statusBarHeight: getApp().globalData.statusBarHeight,
    height: 1370,
    pipeistatus: 1, //默认 匹配中 的状态
    typsstatus: '0', //切换分类显示状态
  },
  /**
   * 点击显示高级信息(二级)
   */
  showtypes: function (index) {
    if (this.data.typeindex != index.currentTarget.dataset.index) {
      this.data.typeindex = index.currentTarget.dataset.index
    } else if (this.data.typeindex == index.currentTarget.dataset.index) {
      this.data.typeindex = 'null'
    }
    this.setData({
      typeindex: this.data.typeindex
    });
    //调用之前关闭
    clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
    //开启定时器 铺获
    this._MonitorPage('#rich_two');

    //调用接口，显示高级信息
    new Request('senior/Get_Senior_List/GetLevel').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
          hei_id: this.data.fatherList[index.currentTarget.dataset.index].hei_id,
          level: '1'
        }
      },
      (res) => {
        // console.log(index.currentTarget.dataset.index)
        this.setData({
          secList: res.data.data
        })
      });

  },
  /**
   * 显示编辑栏
   */
  xiantap: function (param) {
    if (param.currentTarget.id) {
      this.setData({
        seting: null,
      });
    } else {
      this.setData({
        seting: true,
      });
      setTimeout(function () {
        wx.createSelectorQuery().select('#pages').boundingClientRect(function (e) {
          var height = e.height;
          console.log(height);
          this.setData({
            backheight: (height + 40),
          });
        }.bind(this)).exec();
      }.bind(this), 1);
    }
  },
  /**
   * 跳转到编辑独白页面
   */
  gotoadd: function (param) {
    if (param.currentTarget.id == "bianji") {
      if (app.globalData.loginCode == -1) {
        wx.showToast({
          title: '当前是游客身份',
          icon: 'none',
          duration: 1500
        });
        return;
      }
      this.url = "/pages/zhaon_ziown/zhaon_ziown_bianji/zhaon_ziown_bianji";
    } else if (param.currentTarget.id == "vip") {
      this.url = "/pages/zhaon_ziown/zhaon_ziown_vip/zhaon_ziown_vip";
    } else if (param.currentTarget.id == "fankui") {
      this.url = "/pages/zhaon_ziown/zhaon_ziown_fankui/zhaon_ziown_fankui";
    } else if (param.currentTarget.id == "jiufen") {
      this.url = "/pages/zhaon_ziown/zhaon_ziown_jiufen/zhaon_ziown_jiufen";
    } else if (param.currentTarget.id == "dubai") {
      this.url = "/pages/zhaon_ziown/zhaon_ziown_dubai/zhaon_ziown_dubai";
    } else if (param.currentTarget.id == "pipei") {
      if (app.globalData.loginCode == -1) {
        wx.showToast({
          title: '当前是游客身份',
          icon: 'none',
          duration: 1500
        });
        return;
      }
      this.pipei(); //this 匹配修改 为匹配中 不匹配 隐身中  默认是匹配中
      return;
    }
    wx.navigateTo({
      url: this.url,
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: 'test'
        });
        this.setData({
          seting: null,
        });
      }.bind(this),
    });

  },
  /**
   * 修改用户的 匹配中 不匹配 隐身中
   */
  pipei: function () {
    if (this.data.pipeistatus == 1) {
      this.pipeis = 2;
    } else if (this.data.pipeistatus == 2) {
      this.pipeis = 3;
    } else if (this.data.pipeistatus == 3) {
      this.pipeis = 1;
    }
    this.setData({
      pipeistatus: this.pipeis
    });
    wx.showToast({
      title: '修改中...',
      icon: 'loading',
      mask: true,
      duration: 2000
    });
    new Request('customer/Update_Status/Update').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
          yingshengStatus: this.pipeis, //修改的匹配中状态

        }
      },
      (res) => {

      });
  },

  /**
   * 
   * 修改密码
   */
   Updatepwd:function() {
    wx.showModal({
      title: '改密码建设中',
      content: '若遇账号问题，请联系公众号反馈',
      showCancel: false,
    })
   },

  /**切换基本信息、高级信息、印象
   * 
   */
  qietap: function (param) {
    if (param.currentTarget.id == 1) {
      this.data.open = 1;
      let obj = new Object({
        detail: {
          current: 0
        }
      });
      this._bindchange(obj);
    } else if (param.currentTarget.id == 2) {
      this.data.open = 2;
      let obj = new Object({
        detail: {
          current: 1
        }
      });
      this._bindchange(obj);
    } else if (param.currentTarget.id == 3) {
      this.data.open = 3;
      let obj = new Object({
        detail: {
          current: 2
        }
      });
      this._bindchange(obj);
    }
    this.setData({
      open: this.data.open,
    });
  },
  /**
   * 
   * @param {滑块切换按钮} res 
   */
  _bindchange: function (res) {
    if (res.detail.current == 0) {
      this.rich = '#rich_one';
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_one');
    } else if (res.detail.current == 1) {
      this.rich = '#rich_two';
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_two');
    } else if (res.detail.current == 2) {
      this.rich = '#rich_three';
      //调用之前关闭
      clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
      //开启定时器 铺获
      this._MonitorPage('#rich_three');
    }

    this.setData({
      open: (res.detail.current + 1),
      _current: res.detail.current
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(app.globalData.loginCode == -1){
      //用户未注册 提示跳转注册页面
      new Login().LoginDown();
      return;
    }
    let url = wx.getStorageSync("config");
    // 获取当前用户基本信息字段
    new Request('basic/all/Get_All_List/GetList').request(
      () => {
        return {
          userId: app.globalData.userid,
          openid: app.globalData.openid,
        }
      },
      (res) => {
        let reg = new RegExp("女");
        let SQLsex = reg.test(res.data.user_info.choose_gender);
        //用户头像
        let avatar = url.service.host + res.data.user_info.customer_avatar;
        //用户背景墙
        let background = url.service.host + res.data.user_info.background_image;
        //用户状态
        let pipeistatus = res.data.user_info.status;
        this.setData({
          basicList: res.data.data,
          userInfo: res.data.user_info,
          SQLsex: SQLsex,
          avatar: avatar,
          background: background,
          pipeistatus: pipeistatus,
        });

        //获取好友印象列表信息
        new Request('impress/Get_Friend_Impress/GetList').request(
          () => {
            return {
              openid: app.globalData.openid,
              obj_id: res.data.user_info.id, //uid
            }
          },
          (res) => {
            if (res.data.code == 0) {
              this.setData({
                impressScore: 100,
              });
              return;
            }
            this.setData({
              impressList: res.data.data,
              impressScore: res.data.impressScore,
            });
          }
        );

      });
    //获取高级信息列表(标题)
    new Request('senior/Get_Senior_List/GetSeniorInfo').request(
      () => {
        return {
          openid: app.globalData.openid,
          userid: app.globalData.userid,
        }
      },
      (res) => {
        this.setData({
          fatherList: res.data.data
        })
      });
    //获取高级信息列表(二级)
    // new Request('senior/Get_Senior_List/GetLevel').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //       hei_id: '7',
    //       level: '1'
    //     }
    //   },
    //   (res) => {
    //     this.setData({
    //       secList: res.data.data
    //     })
    //   });  



    //获取朋友印象消息
    new Request('Zyfriendinfo/friendyinxianlist').request(
      () => {
        return {
          userId: app.globalData.userid,
        }
      },
      (res) => {
        this.setData({
          yinxiangList: res.data.data
        });
      });

    return;
    new Request('Zyfriendinfo/getfriendinfo').request(
      () => {
        return {
          openid: app.globalData.openid,
          userId: app.globalData.userid,
        }
      },
      (res) => {
        console.log(res);
        this.setData({
          user_tx: res.data.data.userimage
        });

      });
    new Request('Zybasicinfo/getuserbasinfo').request(
      () => {
        return {
          openid: app.globalData.openid,
        }
      },
      (res) => {
        if (res.data.data == '' || res.data.data == null) {
          return;
        }
        this.setData({
          basinfo: res.data.data,
          home: res.data.data.home.replace(/,/g, " "), //定居地信息
          work: res.data.data.work.replace(/,/g, " "), //工作地信息
          huji: res.data.data.huji.replace(/,/g, " "), //户籍地信息
        });
      }
    );

    new Request('Zybasicinfo/getuserbasinfowant').request(
      () => {
        return {
          openid: app.globalData.openid,
        }
      },
      (res) => {
        if (res.data.data == '' || res.data.data == null) {
          return;
        }
        this.setData({
          basinfoa: res.data.data,
          wanthome: res.data.data.home.replace(/,/g, " "), //定居地信息
          wantwork: res.data.data.work.replace(/,/g, " "), //工作地信息
          wanthuji: res.data.data.huji.replace(/,/g, " "), //户籍地信息
        });
      }
    );

  },
  test: function (ele, arr) {
    for (let i = 0; i < arr.length; i++) {
      if (ele == arr[i]) {
        console.log(123 + '----', arr[i])
      } else {
        console.log(234, arr[i])
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //修改头像接口
  UpdateAvatar: function(){
    let url =   wx.getStorageSync("config");
    new Upload().Image(url.service.updateAvatarUrl,
      ()=>{
        return {
          'userid': app.globalData.userid,
        }
      },
      (res)=>{
        if(res.data.code == 0){
          new Request().showToast(res.msg);
          return;
        }
        this.setData({
          avatar: url.service.host + res.data,
        });
      }
    );
  },
  //修改背景墙图片
  UpdateBackground: function(){
    let url =   wx.getStorageSync("config");
    new Upload().Image(url.service.updateBackgroundUrl,
      ()=>{
        return {
          'userid': app.globalData.userid,
        }
      },
      (res)=>{
        if(res.data.code == 0){
          new Request().showToast(res.msg);
          return;
        }
        this.setData({
          background: url.service.host + res.data,
        });
      }
    );
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(app.globalData.loginCode == -1){
      //用户未注册 提示跳转注册页面
      new Login().LoginDown();
      return;
    }
    let url = wx.getStorageSync("config");
    // 获取当前用户基本信息字段
    new Request('basic/all/Get_All_List/GetList').request(
      () => {
        return {
          userId: app.globalData.userid,
          openid: app.globalData.openid,
        }
      },
      (res) => {
        let reg = new RegExp("女");
        let SQLsex = reg.test(res.data.user_info.choose_gender);
        //用户头像
        let avatar = url.service.host + res.data.user_info.customer_avatar;
        this.setData({
          basicList: res.data.data,
          userInfo: res.data.user_info,
          SQLsex: SQLsex,
          avatar: avatar,
        });
      });




    //开启定时器 铺获
    this._MonitorPage('#rich_one');
    if (app.globalData.loginCode == -1) {
      wx.showModal({
        title: '提示',
        content: '当前是游客身份是否返回首页',
        confirmColor: '#58BDDB',
        cancelColor: '#333333',
        confirmText: '是',
        cancelText: '否',
        success(res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/index/index'
            });
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      });
    }
    return;



    //2020年11月24号加入
    new Request('Zyown/pipeiZhi').request(
      () => {
        return {
          openid: app.globalData.openid,
          userId: app.globalData.userid,
        }
      },
      (res) => {
        this.setData({
          pipeistatus: res.data.data
        });
      });


    // new Request('Zhao_Get_User_Info/GetOwnUserInfo').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //     }
    //   },
    //   (res) => {
    //     if (res.data.data == '') {
    //       return;
    //     }
    //     if (res.data.data.text == '' || res.data.data.text == null) {
    //       this.setData({
    //         text: '编写您的独白信息吧~'
    //       });
    //     } else {
    //       this.setData({
    //         text: res.data.data.text
    //       });
    //     }
    //     let sex = "女";
    //     let reg = new RegExp(sex);
    //     let home = res.data.data.home.split(",");
    //     this.setData({
    //       SQLsex: reg.test(res.data.data.sex),
    //       basinfo: res.data.data,
    //       userHome: home[0],
    //       // home: res.data.data.home.replace(/,/g," "),   //定居地信息

    //     });
    //   });

    // new Request('Zybasicinfo/getuserbasinfowant').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //     }
    //   },
    //   (res) => {
    //     this.setData({
    //       basinfoa: res.data.data,

    //     });
    //   });

    // new Request('Zycolumnlist/getcolumnopt').request(
    //   () => {
    //     return {
    //       openid: app.globalData.openid,
    //       userId: app.globalData.userid
    //     }   
    //   },
    //   (res) => {
    //     console.log(res);
    //     // var gjlist=  JSON.parse(res.data);
    //     var gjlist= res.data;
    //     console.log(gjlist);
    //     this.setData({
    //       heightinfo: gjlist
    //     });
    //   });

    /*
      用户高级信息
    */
    wx.request({
      url: 'https://zhiyu.zyfuns.com/api/Zhaon_Get_Height/GetHeightInfo',
      method: 'POST',
      data: {
        openid: app.globalData.openid,
      },
      success: (res) => {
        console.log(res.data.data)
        var gj1list = res.data.data
        this.setData({
          heightinfo: gj1list
        })
      }
    })



  },
  /**
   * 
   * @param {
   *监听 页面长度
   *page : 监听页面id元素
   *LogSetInterval: 初始化巡航铺获
   *LogSetIntNum: 记录巡航铺获15次退出
   *}
   */
  _MonitorPage: function (page, LogSetIntNum = 15, LogSetInterval = new Array()) {
    this.SetIntNumberUserInfo_One = setInterval(function (param) { //巡航铺获
      wx.createSelectorQuery().select(page).boundingClientRect(function (res) {
        if (res == null) {
          clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
          return;
        }
        let heightView = res.height + 300;
        if (app.globalData.windowHeight >= heightView) {
          this.setData({
            height: (app.globalData.windowHeight - app.globalData.statusBarHeight * 2 - 70) + 'px',
          });
          console.log(this.data.height)
        } else {
          this.setData({
            height: (res.height + 100) + 'px',
          });
          console.log(this.data.height)
        }
        LogSetInterval.push(1);
        if (LogSetInterval.length == LogSetIntNum) {
          clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
        }
      }.bind(this)).exec();
    }.bind(this), 98);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.SetIntNumberUserInfo_One); //关闭 铺获
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})